package com.medplus.hubdirect.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.springframework.web.bind.annotation.SessionAttributes;

@Entity
@Table(name="USER_LIST")
@NamedQueries({
@NamedQuery(
    name="findUserByName", query="SELECT u FROM User u where u.userName = :name"),
@NamedQuery(
	name="findUsers", query="SELECT u FROM User u")
})
public class User {

	private long userListId;
	
	@NotNull
	@Size(min=1, max=15)	
	private String userName;
	
	private Set<UserEndPoint> endpoints = new HashSet<UserEndPoint>();
	
	@Id 
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "UserListIdSeq")
	@SequenceGenerator(name="UserListIdSeq",sequenceName="USER_LIST_ID_SEQ", allocationSize=1)
	@Column(name="USER_LIST_ID")	
	public long getUserListId() {
		return userListId;
	}
	public void setUserListId(long userListId) {
		this.userListId = userListId;
	}
	
	@Column(name="USER_NAME")
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	
    @OneToMany(fetch = FetchType.EAGER, mappedBy="user", cascade = CascadeType.ALL)
    // NOT NEEDED @JoinColumn(name = "USER_LIST_ID", nullable = false)	
	public Set<UserEndPoint> getEndpoints() {
		return endpoints;
	}
	public void setEndpoints(Set<UserEndPoint> endpoints) {
		this.endpoints = endpoints;
		/**
		 * cannot do that
		for (UserEndPoint ep : endpoints) {
			ep.setUser(this);
		}
		 */
	}

	
	
}
